home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / mg2a_src.zip / TERMLIB / FGETLR.C next >
C/C++ Source or Header  |  1988-08-23  |  3KB  |  114 lines

  1. /************************************************************************
  2.  *                                      *
  3.  *              Copyright (c) 1982, Fred Fish              *
  4.  *              All Rights Reserved                  *
  5.  *                                      *
  6.  *    This software and/or documentation is released for public          *
  7.  *    distribution for personal, non-commercial use only.          *
  8.  *    Limited rights to use, modify, and redistribute are hereby      *
  9.  *    granted for non-commercial purposes, provided that all          *
  10.  *    copyright notices remain intact and all changes are clearly     *
  11.  *    documented.  The author makes no warranty of any kind with      *
  12.  *    respect to this product and explicitly disclaims any implied    *
  13.  *    warranties of merchantability or fitness for any particular     *
  14.  *    purpose.                                  *
  15.  *                                      *
  16.  ************************************************************************
  17.  */
  18.  
  19.  
  20.  
  21.  
  22. /*
  23.  *  LIBRARY FUNCTION
  24.  *
  25.  *    fgetlr    get logical record from a file
  26.  *
  27.  *  KEY WORDS
  28.  *
  29.  *    fgetlr
  30.  *    string functions
  31.  *
  32.  *  SYNOPSIS
  33.  *
  34.  *    char *fgetlr(bp,bpsize,fp)
  35.  *    char *bp;
  36.  *    int bpsize;
  37.  *    FILE *fp;
  38.  *
  39.  *  DESCRIPTION
  40.  *
  41.  *    Reads the next logical record from stream "fp" into buffer "bp"
  42.  *    until next unescaped newline, "bpsize" minus one characters
  43.  *    have been read, end of file, or read error.
  44.  *    The last character read is followed by a NULL.
  45.  *
  46.  *    A logical record may span several physical records by having
  47.  *    each newline escaped with the standard C escape character
  48.  *    (backslash).
  49.  *
  50.  *    This is particularly useful for things like the termcap
  51.  *    file, where a single entry is too long for one physical
  52.  *    line, yet needs to be treated as a single record.
  53.  *
  54.  *    Returns its first argument unless an end of file or read
  55.  *    error occurs prior to any characters being read.
  56.  *
  57.  *  BUGS
  58.  *
  59.  *    The only way to know if read was terminated due to buffer size
  60.  *    limitation is to test for a newline before the terminating
  61.  *    null.
  62.  *
  63.  */
  64.  
  65. #include <stdio.h>
  66.  
  67.  
  68.  
  69. /*
  70.  *  PSEUDO CODE
  71.  *
  72.  *    Begin fgetlr
  73.  *      If read fails then
  74.  *          Return NULL.
  75.  *      Else
  76.  *          Find out how many characters were read.
  77.  *          Initialize pointer to terminating null.
  78.  *          If last char read was newline then
  79.  *          If newline was escaped then
  80.  *              Replace backslash with the newline.
  81.  *              Replace newline with null.
  82.  *              Read and append more.
  83.  *          End if
  84.  *          End if
  85.  *          Return buffer pointer.
  86.  *      End if
  87.  *    End fgetlr
  88.  *
  89.  */
  90.  
  91. char *fgetlr(bp,bpsize,fp)
  92. char *bp;
  93. int bpsize;
  94. FILE *fp;
  95. {
  96.     int numch;
  97.     char *cp;
  98.  
  99.     if (fgets(bp,bpsize,fp) == NULL) {
  100.       return(NULL);
  101.     } else {
  102.       numch = strlen(bp);
  103.       cp = &bp[numch];
  104.       if (*--cp == '\n') {
  105.       if (numch > 1 && *--cp == '\\') {
  106.           *cp++ = '\n';
  107.           *cp = NULL;
  108.           fgetlr(cp,bpsize-numch+1,fp);
  109.       }
  110.       }
  111.       return(bp);
  112.     }
  113. }
  114.